<template>
	<view>
		<view class="tabs">
			<text :class="{active: index === tabIndex}" v-for="(item, index) in tabs" :key="index" @click="changheTab(index)">{{item}}</text>
		</view>
		<view class="list">
			<service-order-info v-for="(item, index) in list" :key="index" :info="item" @update="onUpdate"/>
		</view>
		<view class="no-data">{{payload.hasmore ? '数据加载中...' : '暂无更多数据'}}</view>
	</view>
</template>

<script>
	import ServiceOrderInfo from '@/components/service-order-info/service-order-info.vue'
	import {utils} from '@/utils'
	import {Order} from '@/apis'
	export default {
		components: {
			'service-order-info': ServiceOrderInfo
		},
		data() {
			return {
				tabIndex: 0,
				tabs: ['全部', '待付款', '待确认', '待完成', '已完成'],
				list: [1,2,3,4,5,6],
				payload: {
					pageSize: 10,
					current: 1,
					hasmore: true
				}
			}
		},
		methods: {
			changheTab (index) {
				this.tabIndex = index;
				this.payload.hasmore = true
				this.payload.current = 1
				this.$mp.page.setData({
					payload: this.payload
				})
				this.fetchList()
			},
			fetchList () {
				utils.wxLoading('加载中...')
				Order.getServiceOrderList({
					data: {
						form: {},
						query: {
							shopId: uni.getStorageSync("shopInfo").id,
							status: [null, 1, 4, 6, 5][this.tabIndex]
						},
						pagination: {
							current: this.payload.current,
							pageSize: this.payload.pageSize
						}
					}
				}).then(res => {
					console.log('获取服务订单列表成功')
					console.log(res.data)
					this.list = res.data.body
				}).finally(() => {
					uni.hideLoading();
					setTimeout(() => {
						uni.stopPullDownRefresh()
					}, 200)
				})
			},
			onUpdate () {
				console.log('刷新')
				this.payload.hasmore = true
				this.payload.current = 1
				this.fetchList()
			}
		},
		mounted() {
			this.fetchList()
			
			this.$on('update', function () {
				console.log('更新函数监听')
			})
		},
		onPullDownRefresh() {
			console.log('获取数据---->')
			this.fetchList()
		}
	}
</script>

<style lang="scss">
page {
	background-color: #EFEFEF;
}
.tabs {
	width: 100%;
	box-sizing: border-box;
	padding: 10rpx;
	background-color: white;
	display: flex;
	overflow-x: auto;
	align-items: center;
	text {
		flex-shrink: 0;
		flex-grow: 0;
		padding: 10rpx 20rpx;
		display: block;
		position: relative;
		&.active:after {
			content: " ";
			position: absolute;
			color: #FF4A4A;
			bottom: 0;
			left: 15rpx;
			right: 15rpx;
			height: 0;
			border-top: #FF4A4A 4rpx solid;
		}
	}
}
.no-data {
  color: #999999;
  height: 50rpx;
  line-height: 50rpx;
  text-align: center;
  font-size: 28rpx;
}

</style>
